Skip to content

Fix/anthropic api compatibility#2603

Merged
danielaskdd merged 2 commits intoHKUDS:mainfrom
skogsbaeck:fix/anthropic-api-compatibility
Jan 19, 2026
Merged

Fix/anthropic api compatibility#2603
danielaskdd merged 2 commits intoHKUDS:mainfrom
skogsbaeck:fix/anthropic-api-compatibility

Conversation

@skogsbaeck
Copy link
Copy Markdown

Description

Fixes Anthropic Claude API compatibility issues in the anthropic.py LLM adapter.

This PR addresses two bugs that prevent proper usage of Anthropic Claude models:

  1. System prompt handling - Anthropic API requires system prompts as top-level parameter, not in messages array
  2. Streaming response Delta attribute checking - prevents AttributeError on events without text attribute

Changes Made

  1. System prompt parameter fix (lines 103-126):

    • Removed code that adds system prompt to messages array with "system" role
    • Added logic to pass system prompt as top-level system parameter to messages.create()
    • Anthropic API only accepts "user" and "assistant" roles in messages array
    • Fixes: Error code: 400 - messages: Unexpected role "system"
  2. Delta attribute checking fix (line 138):

    • Added hasattr(event.delta, "text") check before accessing event.delta.text
    • Not all streaming event types have a text attribute on their delta object
    • Fixes: AttributeError: 'Delta' object has no attribute 'text'

Checklist

  • Changes tested locally
  • [X ] Code reviewed
  • Documentation updated (if necessary)
  • Unit tests added (if applicable)

Additional Notes

Testing performed:

  • Tested with claude-sonnet-4-5-20250929 model
  • Verified entity extraction and knowledge graph construction work correctly
  • Confirmed streaming responses are properly consumed
  • Tested with both empty and non-empty system prompts

API Documentation Reference:

Breaking Changes: None - this is a bug fix that makes the adapter work as intended.

Compatibility: Compatible with all current Anthropic Claude models (Claude 3, Claude 3.5, Claude Opus 4.5, etc.)

…cking in stream response

    Added hasattr check for 'text' attribute before accessing event.delta.text
    Not all Delta events have a 'text' attribute (e.g., message_delta vs content_block_delta)
  Fixes streaming response AttributeError and 400 BadRequest errors when using Anthropic Claude models.
… parameter instead of adding to messages array Anthropic API only accepts 'user' and 'assistant' roles in messages array. System prompts must be passed as a top-level parameter to messages.create()
@danielaskdd
Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielaskdd danielaskdd merged commit 4d987a2 into HKUDS:main Jan 19, 2026
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants